package com.wyz.socket.core.impl.scheduler;

import com.wyz.socket.core.impl.Connector;

import java.util.concurrent.TimeUnit;

public class IdleTimeoutSchedulerJob extends SchedulerJob{

    public IdleTimeoutSchedulerJob(long idleTimesOut, TimeUnit unit, Connector connector) {
        super(idleTimesOut, unit, connector);
    }

    @Override
    public void run() {
        long lastActiveTime = connector.getLastActiveTime();
        long maxTimeoutMilliseconds = this.idleTimesoutMilllsSeconds ;
        long nextDelay = maxTimeoutMilliseconds - (System.currentTimeMillis() - lastActiveTime) ;
        if(nextDelay <= 0 ){
            schedule(maxTimeoutMilliseconds);
            try{
                connector.timeoutEvent() ;
            }catch (Throwable throwable){
                connector.timeoutEventException(throwable) ;
            }
        }else{
            schedule(nextDelay);
        }

    }
}
